package test;

//jdom xml api
import org.jdom.*;   
import org.jdom.input.SAXBuilder;

// java io
import java.io.*;

// java.net
import java.net.MalformedURLException;
import java.net.URL;

// java utility 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SwoogleSearchEngine extends SDGView {

	public SwoogleSearchEngine (String keywords, String ontologyType) 
	{
		super(keywords, ontologyType);
		//listOfUri = new ArrayList<String>();
	}

	public void search() {
		String terms=new String(searchTerms);
		terms=terms.trim();
		terms=terms.replace(" ", "+");
		String urlString = "http://logos.cs.umbc.edu:8080/swoogle31/q?queryType="+
		"search_swd_ontology&searchString="+terms+"&searchStart=1&key=demo"; 
		SAXBuilder builder = new SAXBuilder();
		try {
			URL url = new URL(urlString);
			Document doc = builder.build(new InputStreamReader(url.openStream()));
			Element root = doc.getRootElement();
			listChildren(root);
			String line;
		}  catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} catch (JDOMException e) {
			System.out.println("fichier xml" + " is not well-formed.");
			System.out.println(e.getMessage());
		} finally {
			// Release the connection.
			//   method.releaseConnection();
		}  
	}

	@SuppressWarnings("unchecked")
	public  void listChildren(Element current) {

		  
		   if(current.getName().equals("SemanticWebDocument")) 
		   	               {
		   	                
		   	                 List attributes = current.getAttributes();
		                     if (!attributes.isEmpty()) {
		                     Iterator iterator = attributes.iterator();
		                     while (iterator.hasNext()) {
		                     Attribute attribute = (Attribute) iterator.next();
		                     String name = attribute.getName();
		                     String value = attribute.getValue();
		                     if (value.endsWith("owl"))  listOfUri.add(value);
		                    	 //count=count+1;
		                    	 //out.println(name+" "+value);
		                                              }
		                                              }
		                      
		                     }

		    List children = current.getChildren();
		    Iterator iterator = children.iterator();
		    while (iterator.hasNext()) {
		      Element child = (Element) iterator.next();
		      listChildren(child);
		    }

		  }

}
